/**
 * udes.ds.rmi
 * BasicEquation.java
 * 3 sept. 08
 */
package udes.ds.agent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Stores an equation of the type kx^n
 * 
 * @author Luc Bergevin
 * @version 1.0
 * @see
 */
public class BasicEquation extends AbstractEquation {

	private static final long serialVersionUID = 1L;

	private final double coefficient;
	private int exponent;

	public BasicEquation() throws NumberFormatException, IOException {
		BufferedReader input = new BufferedReader(new InputStreamReader(
				System.in));
		System.out.println("coefficient:exposant");
		String[] vals = input.readLine().split(":");
		this.coefficient = Double.valueOf(vals[0]);
		this.exponent = Integer.valueOf(vals[1]);
	}

	public BasicEquation(double coefficient, int exponent) {
		super();
		this.coefficient = coefficient;
		this.exponent = exponent;
		if (this.exponent < 0) {
			this.exponent = 0;
		}
	}

	public double getCoefficient() {
		return coefficient;
	}

	public int getExponent() {
		return exponent;
	}

	public void addExponent(int exponent) {
		this.exponent += exponent;
	}

	/**
	 * @see udes.ds.rmi.hw.Equation#getFunctionValue(double)
	 */
	@Override
	public double getFunctionValue(double x) {
		return (Math.pow(x, exponent)) * coefficient;
	}

	/**
	 * @see udes.ds.rmi.hw.AbstractEquation#getUserReadableString()
	 */
	@Override
	public String getUserReadableString() {
		return Double.toString(coefficient) + "x^" + Integer.toString(exponent);
	}

	@Override
	public boolean isTerminal() {
		return true;
	}

	@Override
	public Double[] getNumbers() {
		Double[] d = { coefficient, Double.valueOf(exponent) };
		return d;
	}

	@Override
	public String getFingerprint() {
		return getClass().getSimpleName();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(coefficient);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + exponent;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BasicEquation other = (BasicEquation) obj;
		if (Double.doubleToLongBits(coefficient) != Double
				.doubleToLongBits(other.coefficient))
			return false;
		if (exponent != other.exponent)
			return false;
		return true;
	}
}
